/*
    io_com.c - implmentation for io communication driver 
			   This part represents the communication interface (via I2C)
	
	
	Author: Benedikt Niedermayr (2013)
	

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

	This source code is also a result of my bachelor thesis.
*/

#include "io_com.h"





#define ADDRESS 0x48		  // i2c Address of i/o controller itself!  
#define SYS_ASK_I2C "AA"      //Later we will use a own header file for commands!!
#define SYS_ANSWER_SIZE	1
#define RECV_BYTES 2


/**************COMMUNICATION INTERFACE SPECIFIC DEFINITIONS **********/
/*********************************************************************/


static const struct i2c_device_id io_com_id[] = {
	{ "io_com1", 1, },
	{ }
};
MODULE_DEVICE_TABLE(i2c, io_com_id);


/**
 * io_send - send data to connected io processor. This is a function handler.
 * We have to set the global_pointer->io_handler_send.
 * @iop:	global io system data
 * @buff: buffer to be sent
 * @count: length of buffer 
 * Returns negative errno, or else 0.
 */
int io_i2c_send(struct io_com *iop, char *buff, int count)
{

	/* lock this critical access to global element */
	mutex_lock(&iop->async_lock);
	
	if(!count) {
		printk("Sendbuffer length is zero!\n");
		return -EIO;
	}

	if(i2c_master_send((struct i2c_client *)iop->hw_driver, buff, count))
		return -EIO;

	/* unlock this critical part*/
	mutex_unlock(&iop->async_lock);

	return 0;

}



/**
 * io_recv - receive data from connected io processor. This is a function handler.
 * We have to set the global_pointer->io_handler_recv.
 * @iop:	global io system data
 * Returns negative errno, or else 0.
 */
int io_i2c_recv(struct io_com *iop, char *buff, int count)
{
	/* Wich interface should be used?
     * This is depended on interface number of io_com data
     */
	if(i2c_master_recv((struct i2c_client *)iop->hw_driver, buff, count))
		return -EIO;

	return 0;
}


static int __devinit io_com_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	struct io_com *iop; 		
 	if(global_pointer == NULL) { 		
 	iop = global_pointer; 		
 	printk("No global_pointer found!\n"); 		
 	return -EFAULT; 		
 	} 		
 			
 	printk("Starting io_com_probe function\n"); 		
 			
 	/* set hw_driver pointer to i2c_driver ===>client->driver*/ 		
 	iop->client = client; 		
	printk("iop->client Adress is:%p\n",iop->client);
	return 0;		
}


static int io_com_remove(struct i2c_client *client)
{
	printk("Starting io_com_remove function\n");
	return 0;
}


/* driver definitions.
 * Name and function handlers.
 */
static struct i2c_driver io_i2c_driver = {
	.driver = {
		.name	= "io_com",
	},
	.probe		= io_com_probe,
	.remove		= io_com_remove,
	.id_table	= io_com_id,
};


/* driver init function
 */
static int __init io_com_init(void)
{	
	struct i2c_board_info *board_info;
	struct i2c_adapter *i2c_adap;
	struct io_com *iop;

	/* set important i2c driver specific data
     * other way to init this data, is to set 
     * the i2c board_info in setup code.
     */
	board_info = kmalloc(sizeof(*board_info), GFP_KERNEL); 
	if(board_info == NULL) {	
		printk("Error while allocate memory\n");	
		return -ENOMEM;
	}

	i2c_adap = i2c_get_adapter(1);
	strlcpy(board_info->type, "io_com1", I2C_NAME_SIZE);
	board_info->addr = ADDRESS;
	i2c_new_device(i2c_adap, board_info);
 	
	if(i2c_new_device == NULL) {
		kfree(board_info);
		printk("Error while setting new i2c_device\n");
		return -EIO;
	} 


	/* Set recv and send handler functions */
	global_pointer->io_handler_send = io_i2c_send;
	global_pointer->io_handler_recv = io_i2c_recv; 
	printk("I/O Communication driver over I²C loaded!\n");
	return i2c_add_driver(&io_i2c_driver);
}



static void __exit io_com_exit(void)
{
	
	if(global_pointer == NULL) {
		printk("No global_pointer found!\n");
		return;
	}	
	i2c_unregister_device(global_pointer->client);
	i2c_del_driver(&io_i2c_driver);
}
module_init( io_com_init );
module_exit( io_com_exit );

MODULE_AUTHOR("Benedikt Niedermayr");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("i/o controller i2c communication interface");
MODULE_SUPPORTED_DEVICE("none");


